package claim.fuzzy;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/** A linear membership function.
 * @author kinkeldey
 *
 */
public class LinearMembershipFunction implements MembershipFunction {
	
	private static final Log LOG = LogFactory
			.getLog(LinearMembershipFunction.class);
	
	private float start = 0;
	private float stop = 1;

	public LinearMembershipFunction() {
		this(0.f, 1.f);
	}
	
	public LinearMembershipFunction(float start, float stop) {
		this.start = start;
		this.stop = stop;
	}
	
	@Override
	public float getValue(float t) {
		if (t < 0 || t > 1) {
			throw new IllegalArgumentException("coordinate must be in interval [0.0, 1.0].");
		} else if (t < start){
			return 1;
		} else if (t > stop){
			return 0;
		} else {
			float interval = stop - start;
			float slope = 1 / interval;
			return 1 - slope * (t-start);
		}
	}
}
